//厦门大学计算机专业 | 前华为工程师
//专注《零基础学编程系列》  http://lblbc.cn/blog
//包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
//公众号：蓝不蓝编程
Page({
  data: {
    isTake: false,
    show: false,
    isAuth: wx.getStorageSync("isAuth")
  },
  move: function (t) {
    var canvas = this.cxt;
    var width = this.w;
    var height = this.h;
    var e = t.touches[0].y;
    canvas.clearRect(0, 0, width, height);
    var h = Math.atan((e - height / 2) / t.touches[0].x);
    this.n = h / Math.PI * 180;
    canvas.translate(.04 * width, height / 2);
    canvas.moveTo(0, 0);
    canvas.lineTo(.84 * width * Math.cos(h), .84 * width * Math.sin(h));
    canvas.stroke();
    canvas.beginPath();
    canvas.arc(.87 * width * Math.cos(h), .87 * width * Math.sin(h), .03 * width, 0, 2 * Math.PI);
    canvas.stroke();
    canvas.fillStyle = "rgba(255, 255, 255, 0.5)";
    canvas.fill();
    canvas.moveTo(.9 * width * Math.cos(h), .9 * width * Math.sin(h));
    canvas.lineTo(.95 * width * Math.cos(h), .95 * width * Math.sin(h));
    canvas.stroke();
    canvas.rotate(.5 * Math.PI);
    canvas.textAlign = "center";
    canvas.fillStyle = "#000";
    canvas.font = "bold 30px Arial";
    canvas.fillText((90 + this.n).toFixed(1) + "°", 0, .4 * -width);
    canvas.font = "bold 20px Arial";
    canvas.fillText("(" + (90 - this.n).toFixed(1) + "°)", 0, .3 * -width);
    canvas.draw();
  },
  drawbg: function () {
    var bgCanvas = this.bgcanvas;
    var width = this.w;
    var height = this.h;
    bgCanvas.save();
    bgCanvas.translate(.04 * width, height / 2);
    bgCanvas.fillStyle = "rgba(255,255,255,0.5)";
    bgCanvas.arc(0, 0, .8 * width, .5 * Math.PI, 1.5 * Math.PI, true);
    bgCanvas.closePath();
    bgCanvas.stroke();
    bgCanvas.fill();
    bgCanvas.beginPath();
    bgCanvas.fillStyle = "#418DF9";
    bgCanvas.arc(0, 0, 5, 0, 2 * Math.PI, true);
    bgCanvas.fill();
    for (var s = Math.PI / 180, e = 1; e < 181; e++) {
      bgCanvas.beginPath();
      bgCanvas.rotate(s);
      if (e % 10 === 0 && 180 !== e) {
        bgCanvas.moveTo(0, .73 * -width);
        bgCanvas.textAlign = "center";
        bgCanvas.font = "bold 12px Arial";
        bgCanvas.fillText(e, 0, .68 * -width);
      } else if (e % 5 === 0) {
        bgCanvas.moveTo(0, .75 * -width);
      } else {
        bgCanvas.moveTo(0, .77 * -width);
      }
      bgCanvas.lineTo(0, .8 * -width);
      bgCanvas.stroke();
    }
    bgCanvas.restore();
    bgCanvas.draw();
  },
  drawPointer: function () {
    var canvas = this.cxt;
    var width = this.w;
    var i = this.n;
    canvas.moveTo(0, 0);
    canvas.lineTo(.84 * width * Math.cos(i / 180 * Math.PI), .84 * width * Math.sin(i / 180 * Math.PI));
    canvas.stroke();
    canvas.beginPath();
    canvas.arc(.87 * width * Math.cos(i / 180 * Math.PI), .87 * width * Math.sin(i / 180 * Math.PI), .03 * width, 0, 2 * Math.PI);
    canvas.stroke();
    canvas.fillStyle = "rgba(255, 255, 255, 0.5)";
    canvas.fill();
    canvas.moveTo(.9 * width * Math.cos(i / 180 * Math.PI), .9 * width * Math.sin(i / 180 * Math.PI));
    canvas.lineTo(.95 * width * Math.cos(i / 180 * Math.PI), .95 * width * Math.sin(i / 180 * Math.PI));
    canvas.stroke();
    canvas.font = "bold 30px Arial";
    canvas.textAlign = "center";
    canvas.fillStyle = "#000";
    canvas.rotate(.5 * Math.PI);
    canvas.fillText((90 + i).toFixed(1) + "°", 0, .4 * -width);
    canvas.font = "bold 20px Arial";
    canvas.fillText("(" + (90 - i).toFixed(1) + "°)", 0, .3 * -width);
    canvas.draw();
  },
  reset: function () {
    this.bgcanvas.drawImage("../../images/BackGround.png", 0, 0, 400, 1000, 0, 0, this.width, this.h),
      this.drawbg(),
      this.setData({ show: false, isTake: false });
  },
  close: function () {
    this.setData({ show: false });
    this.bgcanvas.drawImage("../../images/BackGround.png", 0, 0, 400, 1000, 0, 0, this.width, this.h),
      this.drawbg();
  },
  show: function () {
    var t = this;
    if (wx.getStorageSync("isAuth") !== false) {
      if (wx.getStorageSync("isAuth") === true) {
        this.setData({ show: true });
        this.bgcanvas.clearRect(0, 0, this.width, this.h);
        this.drawbg();
      } else if (wx.getStorageSync("isAuth") === "") {
        wx.authorize({
          scope: "scope.camera",
          success: function () {
            t.setData({ show: true, isAuth: true });
            wx.getSetting({
              success: function (t) {
                wx.setStorageSync("isAuth", t.authSetting["scope.camera"]);
              }
            });
            t.bgcanvas.clearRect(0, 0, t.width, t.h);
            t.drawbg();
          },
          fail: function () {
            wx.setStorageSync("isAuth", false);
          }
        });
      }
    } else {
      wx.openSetting({
        success: function (t) {
          wx.setStorageSync("isAuth", t.authSetting["scope.camera"]);
        }
      });
    }
  },
  takePhoto: function (t) {
    var a = this;
    if (this.data.isTake) {
      this.bgcanvas.clearRect(0, 0, this.width, this.h);
      this.drawbg();
      this.setData({ show: true, isTake: false });
    } else {
      wx.createCameraContext().takePhoto({
        quality: "high",
        success: function (t) {
          a.bgcanvas.drawImage(t.tempImagePath, 0, 0, a.width, a.h);
          a.drawbg();
          a.setData({ show: false, isTake: true });
        }
      });
    }
  },
  onLoad: function () {
    wx.showShareMenu();
    this.n = 0;
    var t = wx.getSystemInfoSync();
    t.windowHeight > 1.6 * t.windowWidth ? this.w = .8 * t.windowWidth : this.w = t.windowHeight / 2;
    this.h = t.windowHeight;
    this.width = t.windowWidth;
    this.cxt = wx.createCanvasContext("secCanvas");
    this.bgcanvas = wx.createCanvasContext("firstCanvas");
    this.bgcanvas.drawImage("../../images/BackGround.png", 0, 0, 400, 1000, 0, 0, this.width, this.h);
    this.cxt.translate(.04 * this.w, this.h / 2);
    this.drawbg();
    this.drawPointer();
  },
  jumpMeasure: function () {
    wx.navigateBack();
  }
});